package com.zhouxiaoge;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 周明阳
 * mail:gqzmy@outlook.com
 * 时间:2018-04-02 15:44
 */

@WebServlet(name = "AuthenticatedServlet", urlPatterns = "/authenticated")
public class AuthenticatedServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Subject subject = SecurityUtils.getSubject();
        request.setAttribute("subject",subject);
        System.out.println(subject.getPrincipal());
        if (subject.isAuthenticated()) {
            request.getRequestDispatcher("/WEB-INF/jsp/authenticated.jsp").forward(request, response);
        } else {
            request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
        }
    }
}
